/*
 *  Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 * 
 *  Licensed under the GNU General Public License v3 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.gnu.org/licenses/gpl-3.0.html
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package xperiment.metaphor.model.entity;

import xperiment.metaphor.model.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import xperiment.metaphor.model.contact.Address;

/**
 * Site
 * @author Mark Ashworth
 * @version 1.0.0
 */
@Entity
@Table(name="site")
public class Site extends AbstractBean implements PersistentMutable, Named, Comparable {
    private static final long serialVersionUID = 1L;

    /* The unique id of the database object */
    @Id @GeneratedValue(strategy=GenerationType.AUTO) 
    @Column(name = "site_id", unique = true, nullable = false)
    private Long id = null;
    
    /* The name of the site */
    @Column(name="site_name", unique=true, nullable=false)
    private String name;
    
    /* The physical address of the site */
    private Address physicalAddress = null;
    
    /* Whether the site is enabled */
    @Column(name = "enabled", nullable = false)
    private boolean enabled = true;
    
    /**
     * Default constructor
     */
    public Site() {
        super();
    }

    /**
     * Constructs the site with the name and address
     */
    public Site(String name, Address physicalAddress) {
        super();
        setName(name);
        setPhysicalAddress(physicalAddress);
    }
    
    /**
     * The unique id of the database object
     * @return Long
     */
    @Override
    public Long getId() {
        return this.id;
    }

    /**
     *  The unique id of the database object
     * @param id The new value
     */
    @Override
    public void setId(Long id) {
        Long old = getId();
        this.id = id;
        firePropertyChange("id", old, getId());
    }
    
    /**
     * The name of the site
     * @return String
     */
    @Override
    public String getName() {
        return this.name;
    }

    /**
     * The name of the site
     * @param name The new value
     */
    public void setName(String name) {
        String old = getName();
        this.name = name;
        firePropertyChange("name", old, getName());
    }

    /**
     * The physical address of the site
     * @return Address
     */
    public Address getPhysicalAddress() {
        return physicalAddress;
    }

    /**
     * The physical address of the site
     * @param physicalAddress The new value
     */
    public void setPhysicalAddress(Address physicalAddress) {
        Address old = getPhysicalAddress();
        this.physicalAddress = physicalAddress;
        firePropertyChange("physicalAddress", old, getPhysicalAddress());
    }

    /**
     * Whether the site is enabled
     * @param enabled The new value
     */
    public void setEnabled(boolean enabled) {
        boolean old = isEnabled();
        this.enabled = enabled;
        firePropertyChange("enabled", old, isEnabled());
    }

    /**
     * Whether the site is enabled
     * @return boolean
     */
    public boolean isEnabled() {
        return this.enabled;
    }
  
    /**
     * Returns the name as the String representation of the department
     * @return String
     */
    @Override
    public String toString() {
        return getName();
    }
    
    /**
     * Returns the XML representation of the department<br />
     * &lt;site class="xperiment.metaphor.model.organization.Site" id="1" name="Site X" enabled="true" &gt;<br />
     * &lt;physical_address&gt;<br />
     * &lt;address01&gt;Line 01&lt;/address01&gt;<br />
     * &lt;address02&gt;Line 02&lt;/address02&gt;<br />
     * &lt;address03&gt;Line 03&lt;/address03&gt;<br />
     * &lt;postal_code&gt;Line 01&lt;/postal_code&gt;<br />
     * &lt;/physical_address&gt;<br />
     * &lt;/site&gt;<br />
     * @return String
     */
    public String toXML() {
        return "<site class='" 
                + getClass().getName() 
                + "' id='" + getId()
                + "' name='" + getName()
                + "' enabled='" + isEnabled()
                + "' >"
                + (getPhysicalAddress() == null ? "" : getPhysicalAddress().toXML("physical_address"))
                + "</site>";
    }

    /**
     * Calculates the equality of the objects based upon their names
     * @param obj The other object
     * @return boolean
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Site other = (Site) obj;
        if (this.name != other.name && (this.name == null || !this.name.equals(other.name))) {
            return false;
        }
        return true;
    }

    /**
     * Claculates the hash code of the site based upon the name
     * @return int
     */
    @Override
    public int hashCode() {
        int hash = 7;
        hash = 41 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    /**
     * Compares the objects based upon the name
     * @param o The other object
     * @return int
     */
    public int compareTo(Object o) {
        
        if (o != null && o instanceof Site) {
			Site rhs = (Site) o;
			return this.getName().compareTo(rhs.getName());
		}
		return -1;
    }
}
